<template>
  <div>
    <el-card class="box-card">
      <span>
        <i class="iconfont icon-dingdan1 icon"></i>产品类型数据列表
      </span>
      <el-button style="float: right; padding: 3px" type="primary" @click="handleAddAttr">添加</el-button>
    </el-card>
    <el-table class="list" :data="attrList" border style="width: 100%" align="center">
      <el-table-column prop="id" label="编号" width="100"></el-table-column>
      <el-table-column label="分类名称" prop="name"></el-table-column>

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--2.分页的标签-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="listQuery.pageNum"
      :page-sizes="[5, 10]"
      :page-size="listQuery.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <el-form ref="productAttrForm" :model="productAttrCate" :rules="rules">
        <el-form-item label="类型名称">
          <el-input v-model="productAttrCate.name" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchAttrList,
  deleteProductAttr,
  createProductAttr,
  updateProductAttr
} from "@/api/product";
export default {
  name: "productAttr",
  data: function() {
    return {
      attrList: [],
      total: 0,
      listQuery: {
        pageNum: 1, //商品列表显示的页码，默认是第一页
        pageSize: 5 //每页显示的数量
      },
      dialogVisible: false, //弹窗是否显示
      productAttrCate: {
        name: "", //类型名称
        id: null //类型id
      },
      rules: {
        name: [{ required: true, message: "请输入类型名称", trigger: "blur" }]
      },
      title: ""
    };
  },
  mounted: function() {
    this.getAttrList();
  },
  methods: {
    //3.写对应的函数
    handleSizeChange: function(value) {
      console.log("每页的数据发生变化时");
      this.listQuery.pageSize = value;
      this.listQuery.pageNum = 1;
      this.getAttrList();
    },
    handleCurrentChange: function(value) {
      console.log("当前页发生变化时");
      this.listQuery.pageNum = value;
      this.getAttrList();
    },
    //4.需要封装获取数据的函数
    getAttrList: function() {
      fetchAttrList(this.listQuery).then(res => {
        console.log(res);
        this.attrList = res.data;
        this.total = res.total;
      });
    },
    handleDelete: function(row) {
      //先确认是否要删除
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          //再调用删除接口
          //删除类型数据
          deleteProductAttr({
            id: row.id
          }).then(res => {
            console.log(res);
            if (res.status == 200) {
              //数据重新获取
              this.getAttrList();
            } else {
              this.$message({
                type: "error",
                message: "删除报错"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //修改函数
    handleUpdate: function(row) {
      console.log(row);
      this.dialogVisible = true;
      this.title = "修改类型";
      this.productAttrCate.name = row.name;
      this.productAttrCate.id = row.id;
    },
    //表单提交的函数
    handleConfirm() {
      console.log("表单提交了");
      this.$refs.productAttrForm.validate(valid => {
        if (valid) {
          if (this.title == "添加类型") {
            //添加
            //参数 name
            createProductAttr({ name: this.productAttrCate.name }).then(res => {
              console.log(res);
              if (res.type == "success") {
                //1.成功提示
                this.$message({
                  message: "添加成功",
                  type: "success",
                  duration: 1000
                });

                //3.页面数据刷新
                this.getAttrList();
                //2.关闭弹出框
              }
              this.dialogVisible = false;
            });
          } else {
            //修改
            updateProductAttr({
              name: this.productAttrCate.name,
              id: this.productAttrCate.id
            }).then(res => {
              console.log(res);
              if (res.type == "success") {
                //1.成功提示
                this.$message({
                  message: "修改成功",
                  type: "success",
                  duration: 1000
                });

                //3.页面数据刷新
                this.getAttrList();
                //2.关闭弹出框
              }
              this.dialogVisible = false;
            });
          }
        } else {
          return false;
        }
      });
      //调用添加api
    },
    //添加函数
    handleAddAttr() {
      this.dialogVisible = true;
      this.title = "添加类型";
    }
  }
};
</script>

<style scoped>
.input-width {
  width: 60%;
}
</style>
